<template>
  <div class="view-box info-box">
    <info-title :title="infoTitle" icon="el-icon-s-custom"></info-title>
    <div class="info-content">
      <div style="width: 50%; margin: 10px auto">
        <el-form ref="formName" :model="form" label-width="80px" :rules="rules">
          <el-form-item label="权限名称" prop="name">
            <el-input class="info-text" v-model="form.name"></el-input>
          </el-form-item>
          <el-form-item label="url" prop="url">
            <el-input class="info-text" v-model="form.url"></el-input>
          </el-form-item>
          <el-form-item label="路径">
            <el-input class="info-text" v-model="form.path"></el-input>
          </el-form-item>
          <el-form-item label="上级权限">
            <tree-select
              :props="treeSelect.props"
              :options="treeSelect.options"
              :value="treeSelect.valueId"
              :clearable="treeSelect.isClearable"
              :accordion="treeSelect.isAccordion"
              @getValue="getValue($event)"
            ></tree-select>
          </el-form-item>
          <el-form-item label="图标">
            <e-icon-picker v-model="form.icon" />
          </el-form-item>
          <el-form-item label="权限描述">
            <el-input class="info-text" v-model="form.desc"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="onSubmit('formName')"
              >立即创建</el-button
            >
            <el-button @click="goBack">取消</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
// 导入标题组件
import infoTitle from "../../../../components/common/info-title";
import { getAccesses, addAccess, updateAccess } from "../../../../api/access";
import treeSelect from "../../../../components/tree/treeSelect";
export default {
  data() {
    return {
      // 标题信息
      infoTitle: "新增权限",
      //   表单信息
      form: {
        name: "",
        url: "",
      },
      //   当前是修改还是新增
      type: "add",
      // 表单信息校验
      rules: {
        name: [
          { required: true, message: "权限名称不能为空", trigger: "blur" },
        ],
        url: [{ required: true, message: "url不能为空", trigger: "blur" }],
      },
      treeSelect: {
        isClearable: true, // 可清空（可选）
        isAccordion: false, // 可收起（可选）
        valueId: "0", // 初始ID（可选）
        props: {
          // 配置项（必选）
          value: "id",
          label: "name",
          children: "children",
        },
        options: [],
      },
    };
  },
  components: { infoTitle, treeSelect },
  mounted() {
    if (JSON.stringify(this.$route.params) != "{}") {
      this.form = this.$route.params;
      this.infoTitle = "修改角色";
      this.type = "update";
      this.treeSelect.valueId = this.form.pid
    }
    let that = this;
    getAccesses().then((res) => {
      if (res.success) this.treeSelect.options = res.data;
    });
  },
  methods: {
    // 取值
    getValue(value) {
      this.form.pid = value;
      console.log(this.form.pid );
    },
    onSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.submitData();
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    submitData() {
      if (this.type == "update") {
        updateAccess(this.form).then((res) => {
          if (res.success) this.$router.go(-1);
        });
      } else {
        addAccess(this.form).then((res) => {
          if (res.success) this.$router.go(-1);
        });
      }
    },
    goBack() {
      this.$router.go(-1);
    },
  },
};
</script>

<style>
.view-box {
  padding: 10px;
}
.info-content {
  padding: 10px;
}
</style>